package com.ruoyi.goods.GoodsStore.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.goods.GoodsStore.domain.GoodsStore;
import com.ruoyi.goods.GoodsStore.domain.StockBill;
import com.ruoyi.goods.GoodsStore.domain.StockBillItem;
import com.ruoyi.goods.GoodsStore.mapper.StockBillMapper;
import com.ruoyi.goods.GoodsStore.service.IStockBillItemService;
import com.ruoyi.goods.GoodsStore.service.IStockBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;



/**
 * 出入库单据Service业务层处理
 *
 * @author godlei
 * @date 2022-11-27
 */
@Service
public class StockBillServiceImpl extends ServiceImpl<StockBillMapper, StockBill> implements IStockBillService {
    @Autowired
    private IStockBillItemService stockBillItemService;
    @Autowired
    private GoodsStoreServiceImpl goodsStoreService;
    /**
     * 查询出入库单据列表
     *
     * @param stockBill 出入库单据
     * @return 出入库单据
     */
    @Override
    public List<StockBill> selectStockBillList(StockBill stockBill) {
        return getBaseMapper().selectStockBillList(stockBill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchByIds(Collection<?> list) {
        UpdateWrapper<StockBill> wrapper = new UpdateWrapper<>();
        wrapper.in("id",list).set("status",-1);
        for (Object o : list) {
            StockBill stockBill = super.getById((Long) o);
            List<StockBillItem> item1 = stockBillItemService.selectStockBillItemByBillId(stockBill.getId());
            if(stockBill.getType().equals(0)){
                for (StockBillItem stockBillItem : item1) {
                    stockBillItem.setState(-1);
                    GoodsStore goodsStore = goodsStoreService.selectGoodsStore(stockBillItem.getGoodsId(),Long.valueOf(stockBill.getStoreId()));
                    if(goodsStore.getAmounts() - stockBillItem.getAmounts() < 0){
                        throw new ServiceException("物品库存数量不足，无法执行操作");
                    }
                    UpdateWrapper<GoodsStore> wrapper1 = new UpdateWrapper<>();
                    wrapper1.set("amounts",(goodsStore.getAmounts() - stockBillItem.getAmounts())).eq("goods_id",stockBillItem.getGoodsId()).eq("store_id",stockBill.getStoreId());
                    goodsStoreService.update(wrapper1);
                    stockBillItemService.updateById(stockBillItem);
            }
            }
            if(stockBill.getType().equals(1)){
                for (StockBillItem stockBillItem : item1) {
                    stockBillItem.setState(-1);
                    stockBillItemService.updateById(stockBillItem);
                    GoodsStore goodsStore = goodsStoreService.selectGoodsStore(stockBillItem.getGoodsId(),Long.valueOf(stockBill.getStoreId()));
                    if(goodsStore != null){
                        UpdateWrapper<GoodsStore> wrapper2 = new UpdateWrapper<>();
                        wrapper2.set("amounts",(goodsStore.getAmounts() + stockBillItem.getAmounts())).eq("goods_id",stockBillItem.getGoodsId()).eq("store_id",stockBill.getStoreId());
                        goodsStoreService.update(wrapper2);
                    }else{
                        GoodsStore goodsStore1 = new GoodsStore();
                        goodsStore1.setGoodsId(stockBillItem.getGoodsId());
                        goodsStore1.setStoreId(Long.valueOf(stockBill.getStoreId()));
                        goodsStore1.setAmounts(Math.toIntExact(stockBillItem.getAmounts()));
                        goodsStoreService.save(goodsStore1);
                    }

                }

            }
        }
        return super.update(null,wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(StockBill stockBill) {
        stockBill.setOperateDate(new Date());
        stockBill.setOperatorId(SecurityUtils.getUserId());
        stockBill.setType(0);
        boolean save = super.save(stockBill);
        if(save){
            for (StockBillItem stockBillItem : stockBill.getItem()) {
                stockBillItem.setBillId(stockBill.getId());
                stockBillItem.setState(0);
                GoodsStore goodsStore = goodsStoreService.selectGoodsStore(stockBillItem.getGoodsId(),Long.valueOf(stockBill.getStoreId()));
                if(goodsStore != null){
                    UpdateWrapper<GoodsStore> wrapper1 = new UpdateWrapper<>();
                    wrapper1.set("amounts",(goodsStore.getAmounts() + stockBillItem.getAmounts()))
                            .eq("goods_id",stockBillItem.getGoodsId())
                            .eq("store_id",stockBill.getStoreId());
                    goodsStoreService.update(wrapper1);
                }else {
                    GoodsStore goodsStore1 = new GoodsStore();
                    goodsStore1.setGoodsId(stockBillItem.getGoodsId());
                    goodsStore1.setStoreId(Long.valueOf(stockBill.getStoreId()));
                    goodsStore1.setAmounts(Math.toIntExact(stockBillItem.getAmounts()));
                    goodsStoreService.save(goodsStore1);
                }
            }
             return stockBillItemService.saveBatch(stockBill.getItem());
        }else{
            throw new ServiceException("入库失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOut(StockBill stockBill) {
        stockBill.setOperateDate(new Date());
        stockBill.setOperatorId(SecurityUtils.getUserId());
        stockBill.setType(1);
        boolean save = super.save(stockBill);
        if(save){
            for (StockBillItem stockBillItem : stockBill.getItem()) {
                stockBillItem.setBillId(stockBill.getId());
                stockBillItem.setState(0);
                GoodsStore goodsStore = goodsStoreService.selectGoodsStore(stockBillItem.getGoodsId(),Long.valueOf(stockBill.getStoreId()));
                if(goodsStore != null){
                    if(goodsStore.getAmounts() - stockBillItem.getAmounts() < 0){
                        throw new ServiceException("该仓库库存不足，出库失败");
                    }
                    UpdateWrapper<GoodsStore> wrapper1 = new UpdateWrapper<>();
                    wrapper1.set("amounts",(goodsStore.getAmounts() - stockBillItem.getAmounts()))
                            .eq("goods_id",stockBillItem.getGoodsId())
                            .eq("store_id",stockBill.getStoreId());
                    goodsStoreService.update(wrapper1);
                }else {
                        throw new ServiceException("该仓库库存不足，出库失败");
                }
            }
            return stockBillItemService.saveBatch(stockBill.getItem());
        }else{
            throw new ServiceException("出库失败");
        }
    }
}
